<?php

/**
 * ECSHOP 程序说明
 * ===========================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ==========================================================
 * $Author: liubo $
 * $Id: sms_sendlist.php 17217 2011-01-19 06:29:08Z liubo $
 */

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
admin_priv('sms_sendlist');
if ($_REQUEST['act'] == 'list')
{
    $listdb = get_sms_sendlist();
    $smarty->assign('ur_here',      $_LANG['07_sms_sendlist']);
    $smarty->assign('full_page',  1);

    $smarty->assign('listdb',      $listdb['listdb']);
    $smarty->assign('filter',       $listdb['filter']);
    $smarty->assign('record_count', $listdb['record_count']);
    $smarty->assign('page_count',   $listdb['page_count']);

    assign_query_info();
    $smarty->display('sms_sendlist.htm');
}
elseif ($_REQUEST['act'] == 'query')
{
    $listdb = get_sms_sendlist();
    $smarty->assign('listdb',      $listdb['listdb']);
    $smarty->assign('filter',       $listdb['filter']);
    $smarty->assign('record_count', $listdb['record_count']);
    $smarty->assign('page_count',   $listdb['page_count']);

    $sort_flag  = sort_flag($listdb['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('sms_sendlist.htm'), '', array('filter' => $listdb['filter'], 'page_count' => $listdb['page_count']));
}
elseif ($_REQUEST['act'] == 'del')
{
    $id = intval($_REQUEST['id']);
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('sms_sendlist') . " WHERE id = '$id' LIMIT 1";
    $db->query($sql);
    $links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
    sys_msg('成功删除', 0, $links);
}

/*------------------------------------------------------ */
//-- 批量删除
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'batch_remove')
{
    /* 检查权限 */
    if (isset($_POST['checkboxes']))
    {
        $sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id " . db_create_in($_POST['checkboxes']);
        $db->query($sql);

        $links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
        sys_msg('成功删除', 0, $links);
    }
    else
    {
        $links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
        sys_msg('没有选中项', 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 批量发送
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'batch_send')
{
    /* 检查权限 */
    if (isset($_POST['checkboxes']))
    {
        $sql = "SELECT s.*,st.tpl_id FROM " . $ecs->table('sms_sendlist') . " AS s ".
          		"JOIN ".$ecs->table('sms_templates')." AS st ON s.template_id = st.id ".
        		"WHERE s.id " . db_create_in($_POST['checkboxes']) . " ORDER BY last_send ASC";
        $res = $db->getAll($sql);
        if (!$res){
        	$links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
        	sys_msg('发送列表为空', 0, $links);
        }
        
        foreach($res as $k=>$row)
        {
            if (!empty($row['id']) && empty($row['phone']))
            {
                $sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id = '$row[id]'";
                $db->query($sql);
                continue;
            }
			$result = send_sms($row['phone'], $row['tpl_id'],array());
           
            if ($result['error']==0)
            {
            	//发送成功

            	//从列表中删除
            	$sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id = '$row[id]'";
                $db->query($sql);
            }
            else
            {
            	//发送出错

            	if ($row['error'] < 3)
            	{
                	$time = gmtime();
                	$sql = "UPDATE " . $ecs->table('sms_sendlist') . " SET error = error + 1, last_send = '$time' WHERE id = '$row[id]'";
                }
                else
                {
                	//将出错超次的纪录删除
                	$sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id = '$row[id]'";
                }
                $db->query($sql);
            }
            
        }

        $links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
        sys_msg('发送完成', 0, $links);
    }
    else
    {
        $links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
        sys_msg('没有选中项', 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 全部发送
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'all_send')
{
	$sql = "SELECT s.*,st.tpl_id FROM " . $ecs->table('sms_sendlist') . " AS s ".
          		"JOIN ".$ecs->table('sms_templates')." AS st ON s.template_id = st.id ".
        		"ORDER BY last_send ASC";
	$res = $db->getAll($sql);
	if (!$res){
		$links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
		sys_msg('发送列表为空', 0, $links);
	}
    foreach ($res as $k=>$row)
    {
        if (!empty($row['id']) && empty($row['phone']))
        {
			$sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id = '$row[id]'";
			$db->query($sql);
			continue;
		}
		$result = send_sms($row['phone'], $row['tpl_id'],array());

    	if ($result['error']==0)
		{
			//发送成功

			//从列表中删除
			$sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id = '$row[id]'";
			$db->query($sql);
		}
		else
		{
			//发送出错

			if ($row['error'] < 3)
			{
				$time = gmtime();
				$sql = "UPDATE " . $ecs->table('sms_sendlist') . " SET error = error + 1, last_send = '$time' WHERE id = '$row[id]'";
			}
			else
			{
				//将出错超次的纪录删除
				$sql = "DELETE FROM " . $ecs->table('sms_sendlist') . " WHERE id = '$row[id]'";
			}
			$db->query($sql);
		}
    }

    $links[] = array('text' => $_LANG['07_sms_sendlist'], 'href' => 'sms_sendlist.php?act=list');
	sys_msg('发送完成', 0, $links);
}

function get_sms_sendlist()
{
    $result = get_filter();
    if ($result === false)
    {
        $filter['sort_by']      = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']   = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $sql = "SELECT count(*) FROM " . $GLOBALS['ecs']->table('sms_sendlist');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT s.*, st.tpl_title, u.user_name FROM " . $GLOBALS['ecs']->table('sms_sendlist') . " s".
          	" LEFT JOIN " . $GLOBALS['ecs']->table('sms_templates') . " st ON s.template_id = st.id" .
          	" LEFT JOIN " . $GLOBALS['ecs']->table('users') . " u ON u.user_id = s.user_id" .
            " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
           " LIMIT " . $filter['start'] . ",$filter[page_size]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $listdb = $GLOBALS['db']->getAll($sql);
    
    foreach ($listdb as $k=>$v){
    	$listdb[$k]['last_send'] = local_date('Y-m-d H:i:s',$v['last_send']);
    }

    $arr = array('listdb' => $listdb, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}
?>